﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using JPW.OnlinePaperSubmissionSystem.Project.Dto;
using Kendo.DynamicLinq;
using System.Data.Entity;
namespace JPW.OnlinePaperSubmissionSystem.Project
{
    
   // [AbpAuthorize(AppPermissions.Pages_Tenant_Audit)]
    public class AuditAppService : AbpZeroTemplateAppServiceBase, IAuditAppService
    {
        private readonly IRepository<Audit, long> _auditRepository;
        private readonly IRepository<Manuscript, long> _manuscriptRepository;
        private readonly IRepository<ManuscriptChecked, long> _manuscriptCheckedRepository;
        public AuditAppService(
            IRepository<Audit, long> auditRepository,
            IRepository<Manuscript, long> manuscriptRepository,
            IRepository<ManuscriptChecked, long> manuscriptCheckedRepository
            )
        {
            _auditRepository = auditRepository;
            _manuscriptRepository = manuscriptRepository;
            _manuscriptCheckedRepository = manuscriptCheckedRepository;
        }

        public DataSourceResult KendoGetAudits(DataSourceRequest input)
        {
         var audits = _auditRepository
            .GetAll()
            .OrderBy(p => p.Id)
            .MapTo<List<AuditListDto>>().AsQueryable()
            .ToDataSourceResult(input);

           return audits;
        }
         public ListResultOutput<AuditListDto> GetAudits(GetAuditInput input)
        {
            var audits = _auditRepository
            .GetAll()
            //.WhereIf(
            //    !input.Filter.IsNullOrEmpty(),
            //    p => p.Name.Contains(input.Filter) ||
            //            p.Code.Contains(input.Filter) 
                        //||p.Performance==Convert.ToInt32(input.Filter)
            //)
            .OrderBy(p => p.Id)
            .ToList();

            return new ListResultOutput<AuditListDto>(audits.MapTo<List<AuditListDto>>());
        }
         public async Task<ListResultOutput<AuditListDto>> GetAuditsAsync()
        {
            var query = await _auditRepository.GetAll().OrderBy(p => p.Id).ToListAsync();

            return new ListResultOutput<AuditListDto>(query.MapTo<List<AuditListDto>>());

        }
        
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Audit_Delete)]
        public async Task DeleteAudit(IdInput input)
        {
            await _auditRepository.DeleteAsync(input.Id);
        }
        
        public async Task CreateOrUpdateAudit(CreateOrUpdateAuditInput input)
        {
            if (input.Audit.ID.HasValue)
            {
                await UpdateAuditAsync(input);
            }
            else
            {
                await CreateAuditAsync(input);
            }
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Audit_Create)]
        public async Task CreateAuditAsync(CreateOrUpdateAuditInput input)
        {
            var audit = input.Audit.MapTo<Audit>();
            await _auditRepository.InsertAsync(audit);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Audit_Update)]
        public async Task UpdateAuditAsync(CreateOrUpdateAuditInput input)
        {
            var audit = input.Audit.MapTo<Audit>();
            await _auditRepository.UpdateAsync(audit);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Audit_Update)]
        public async Task<GetAuditForEditOutput> GetAuditForEdit(NullableIdInput input)
        {
            Audit audit = null;
            if (input.Id.HasValue) //Editing existing role?
            {
                audit = await _auditRepository.FirstOrDefaultAsync(input.Id.Value);
            }
            return new GetAuditForEditOutput
            {
                Audit = audit.MapTo<AuditEditDto>(),
            };
        }
        public async Task CreateAuditStatus(CreateOrUpdateAuditInput input)
        {
            var audit = input.Audit.MapTo<Audit>();
            long AuditId = await _auditRepository.InsertAndGetIdAsync(audit);
            ManuscriptChecked mm = new ManuscriptChecked();
            mm.AuditId = AuditId;
            mm.ManuscriptId = input.ManuscriptId;
            mm.CurUserID = audit.UserId;
            mm.NextUserID = input.NextUserID;
            await _manuscriptCheckedRepository.InsertAsync(mm);
            var item = _manuscriptRepository.FirstOrDefault(x => x.Id == input.ManuscriptId);
            item.ManuscriptStatusID = audit.AuditStatuId;

        }

    }
}

